package com.mc.jf.apicontroller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mc.jf.entity.*;
import com.mc.jf.service.impl.PlanServiceImpl;
import com.mc.jf.service.impl.PlanUserServiceImpl;
import com.mc.jf.utils.DateUtils;
import com.mc.jf.utils.RoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Lin
 * @since 2022-08-19
 */
@RestController
@RequestMapping("/api/plan")
public class PlanApiController {

    @Autowired
    PlanServiceImpl planService;

    @Autowired
    PlanUserServiceImpl planUserService;


    @GetMapping(value = "/add")
    public HttpResponse add(Plan plan) {
        LambdaQueryWrapper<PlanUser> qw = new LambdaQueryWrapper<>();
        qw.eq(PlanUser::getUsername, plan.getUsername());
        PlanUser planUser = planUserService.getOne(qw);
        plan.setUid(planUser.getId());
        if (!StringUtils.hasLength(plan.getEdate())) {
            plan.setEdate(plan.getDate());
        }
        if (planService.save(plan)) {
            return ResUtil.ok(null);
        } else {
            return ResUtil.error("操作失败");
        }
    }

    @GetMapping(value = "/delete")
    public HttpResponse delete(String id) {
        Plan plan = new Plan();
        plan.setId(id);

        if (planService.removeById(plan)) {
            return ResUtil.ok(null);
        } else {
            return ResUtil.error("操作失败");
        }
    }


    @GetMapping(value = "/list")
    public HttpListResponse list(Long page, Long limit, String searchParams) {

        if (page == null) {
            page = 1L;
        }
        if (limit == null) {
            limit = 10L;
        }


        Page<Plan> p = new Page<Plan>(page, limit);
        LambdaQueryWrapper<Plan> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(searchParams)) {
            JSONObject json = JSON.parseObject(searchParams);
            if (json != null) {
                String date = (String) json.get("date");

                if (StringUtils.hasLength(date)) {

                    queryWrapper.ge(Plan::getEdate, date);

                }
            }
        }
        if (!RoleUtil.isAdmin()) {
            queryWrapper.eq(Plan::getUsername, RoleUtil.getUser().getUsername());
            queryWrapper.ge(Plan::getEdate, DateUtils.dateToStr(new Date()));
        }
        queryWrapper.orderByAsc(Plan::getDate);
        queryWrapper.orderByAsc(Plan::getUid);
        planService.page(p, queryWrapper);
        List<Plan> memberList = p.getRecords();
        return ResUtil.list(p.getTotal(), memberList);
    }


    @GetMapping(value = "/list2Month")
    public Object list() {//显示两个月

        String dateTag = "";
        Integer index = 1;
        LambdaQueryWrapper<Plan> planQueryWrapper = new LambdaQueryWrapper<>();
        planQueryWrapper.orderByAsc(Plan::getDate);
        planQueryWrapper.orderByAsc(Plan::getUid);
        planQueryWrapper.ge(Plan::getEdate, DateUtils.dateToStr(new Date()));

        List<Plan> plans = planService.list(planQueryWrapper);
        for (Plan plan : plans) {
            if (!dateTag.equals(plan.getDate())) {

                dateTag = plan.getDate();
                index = 1;

            } else {
                index++;
            }
            plan.setAction(getAction(index, plan));
            plan.setDate(plan.getDate().substring(plan.getDate().indexOf("-")+1).replace("-", "月") + "日");


        }
        return ResUtil.list((long) plans.size(), plans);

    }

    public String getAction(Integer index, Plan plan) {
        StringBuilder stringBuilder = new StringBuilder();
        if (index != null) {
            stringBuilder.append(index + ".");
        }
        stringBuilder.append(plan.getUsername() + plan.getDate().substring(plan.getDate().lastIndexOf("-") + 1) + "日");
        if (!plan.getDate().equals(plan.getEdate())) {

            if (plan.getDate().substring(plan.getDate().indexOf("-") + 1, plan.getDate().lastIndexOf("-"))
                    .equals(plan.getEdate().substring(plan.getEdate().indexOf("-") + 1, plan.getEdate().lastIndexOf("-")))) {
                stringBuilder.append("-" + plan.getEdate().substring(plan.getEdate().lastIndexOf("-") + 1) + "日");
            } else {
                stringBuilder.append("-" + plan.getEdate().substring(plan.getEdate().indexOf("-") + 1, plan.getEdate().lastIndexOf("-")) + "月"
                        + plan.getEdate().substring(plan.getEdate().lastIndexOf("-") + 1) + "日");
            }
        }
        if (StringUtils.hasLength(plan.getAction())) {
            stringBuilder.append("在" + plan.getLocation()  + plan.getAction());
        } else {
            stringBuilder.append("去" + plan.getLocation());
        }
        return stringBuilder.toString();
    }


}
